package utils;

import java.nio.file.*;
import java.util.*;
import library.*;

public class ResourceFileUtils {

    private static final String RESOURCE_FILE = "resources.txt";
    private static final String BORROW_RECORD_FILE = "borrow_records.txt";

    public static void saveResources(List<Resource> resources) {
        try {
            Path path = Paths.get(RESOURCE_FILE);
            if (!Files.exists(path)) Files.createFile(path);

            List<String> lines = new ArrayList<>();
            for (Resource r : resources) {
                String type = r.getClass().getSimpleName();
                String line = String.join(",",
                        r.getId(),
                        type,
                        r.getTitle(),
                        String.valueOf(r.isAvailable())
                );
                lines.add(line);
            }
            Files.write(path, lines);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<Resource> loadResources() {
        List<Resource> resources = new ArrayList<>();
        try {
            Path path = Paths.get(RESOURCE_FILE);
            if (!Files.exists(path)) return resources;

            List<String> lines = Files.readAllLines(path);
            for (String line : lines) {
                String[] parts = line.split(",");
                if (parts.length < 4) continue;

                String id = parts[0];
                String type = parts[1];
                String title = parts[2];
                boolean available = Boolean.parseBoolean(parts[3]);

                Resource resource = null;
                switch (type) {
                    case "Book":
                        resource = new Book(id, title);
                        break;
                    case "Magazine":
                        resource = new Magazine(id, title);
                        break;
                    case "DVD":
                        resource = new DVD(id, title);
                        break;
                    default:
                        continue;
                }
                resource.setAvailable(available);
                resources.add(resource);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resources;
    }

    public static void saveBorrowRecords(Map<String, Borrower> borrowers) {
        try {
            Path path = Paths.get(BORROW_RECORD_FILE);
            if (!Files.exists(path)) Files.createFile(path);

            List<String> lines = new ArrayList<>();
            for (Borrower borrower : borrowers.values()) {
                for (Resource r : borrower.getBorrowedResources()) {
                    lines.add(borrower.getName() + "," + r.getId());
                }
            }
            Files.write(path, lines);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Map<String, List<String>> loadBorrowRecords() {
        Map<String, List<String>> records = new HashMap<>();
        try {
            Path path = Paths.get(BORROW_RECORD_FILE);
            if (!Files.exists(path)) return records;

            List<String> lines = Files.readAllLines(path);
            for (String line : lines) {
                String[] parts = line.split(",");
                if (parts.length != 2) continue;

                String name = parts[0];
                String resourceId = parts[1];

                records.computeIfAbsent(name, k -> new ArrayList<>()).add(resourceId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return records;
    }
}
